﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ProjectFinal {
	public partial class frmCheckIn : Form {
		// Declaration of external function.
		[DllImport("user32.dll")]
		private static extern int SendMessage(int hWnd, int wMsg, int wParam, ref int lParam);

		// Windows message constant for setting the tab stops for a list box
		private const int LB_SETTABSTOPS = 0x192;

		public frmCheckIn() {
			InitializeComponent();
			setListBoxes();
			BusinessRules.listPatrons(listBoxPatronList);
		}

		//sets the tab width for listboxes
		private void setListBoxes() {
			// Set Tabs
			int[] ListBoxTabs = new int[] { 200, 240 };
			int result;

			// Send an LB_SETTABSTOPS message to the ListBox control.
			result = SendMessage(this.listBoxCheckedOut.Handle.ToInt32(),
					LB_SETTABSTOPS, ListBoxTabs.Length, ref ListBoxTabs[0]);
		}

		private void btnExit_Click(object sender, EventArgs e) {
			this.Close();
		}

		private void listBoxPatronList_SelectedIndexChanged(object sender, EventArgs e) {
			if(listBoxPatronList.SelectedIndex != -1)
				BusinessRules.listPatronItems(listBoxCheckedOut, listBoxPatronList.SelectedIndex);
		}

		private void btnCheckIN_Click(object sender, EventArgs e) {
			if(listBoxCheckedOut.SelectedIndex != -1) {
				String temp = listBoxCheckedOut.Items[listBoxCheckedOut.SelectedIndex].ToString();
				String[] parse = temp.Split();
				temp = parse[parse.Length - 2];
				Item item = BusinessRules.getSpecificItem(temp);

				String title = "";
				for(int i = 0; i < parse.Length - 2; ++i) {
					title += parse[i];
					if(i < parse.Length - 3)
						title += " ";
				}

				if(MessageBox.Show(this, "Are you sure you want to check in\n'" + title + "'?",
						"Verify Check In",
						MessageBoxButtons.YesNo,
						MessageBoxIcon.Question)
						 == DialogResult.No)
					return;

				if(item.getType() == MediaType.adultBook || item.getType() == MediaType.childBook)
					//BusinessRules.checkIn(BusinessRules.getPatronInfo(listBoxPatronList.	
					BusinessRules.checkIn(BusinessRules.getPatronInfo(listBoxPatronList.SelectedIndex), (Book)item);
				else
					BusinessRules.checkIn(BusinessRules.getPatronInfo(listBoxPatronList.SelectedIndex), (Media)item);

				BusinessRules.listPatronItems(listBoxCheckedOut, listBoxPatronList.SelectedIndex);
			}
		}

		//checks in all items at once
		private void btnCheckInAll_Click(object sender, EventArgs e) {
			int patronIndex = listBoxPatronList.SelectedIndex;
			if(patronIndex != -1) {

				if(MessageBox.Show(this, "Are you sure you want to check in ALL of " + listBoxPatronList.SelectedItem + "'s items?",
						"Verify Check In",
						MessageBoxButtons.YesNo,
						MessageBoxIcon.Question)
						 == DialogResult.No)
					return;

				foreach(Item i in BusinessRules.getPatronItemList(patronIndex)) {
					if(i.getType() == MediaType.adultBook || i.getType() == MediaType.childBook)
						BusinessRules.checkIn(BusinessRules.getPatronInfo(patronIndex), (Book)i);
					else
						BusinessRules.checkIn(BusinessRules.getPatronInfo(patronIndex), (Media)i);
				}
			}
			listBoxCheckedOut.Items.Clear();
		}
	}
}
